<template>
  <div class="mod-menu">
    <el-form :inline="true" :model="pamars">
      <el-form-item label="单品名称">
        <el-input v-model="pamars.title" placeholder="单品名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="单品分组">
        <!-- <el-select v-model="pamars.goods_group_id" clearable placeholder="请选择">
          <el-option v-for="item in goodsGroupsList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select> -->
        <el-cascader v-model="dataForm.goods_group_id" :options="goodsGroupsList" clearable :show-all-levels="false" @change="getTemplateRow"></el-cascader>
        <!-- </el-form-item> -->
      </el-form-item>
      <el-form-item label="上下架">
        <el-select v-model="pamars.added" clearable placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" row-key="Id" border style="width: 100%; ">
      <el-table-column prop="title" align="center" label="单品名称">
      </el-table-column>
      <el-table-column prop="code" align="center" label="单品编码">
      </el-table-column>
      <el-table-column prop="unit" align="center" label="单品单位">
      </el-table-column>
      <el-table-column prop="goods_group_name" align="center" label="单品分组">
      </el-table-column>
      <el-table-column prop="able_stock" align="center" label="库存">
      </el-table-column>
      <el-table-column prop="added" align="center" label="上下架">
        <template slot-scope="scope">
          <el-switch @change="switchChange(scope.row)" v-model="scope.row.added" active-color="#17B3A3" inactive-color="#B9B9B9">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <!-- <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button> -->
          <el-button type="text" size="small" @click="goOutPutOrInPut(scope.row.id,0)">出库</el-button>
          <el-button type="text" size="small" @click="goOutPutOrInPut(scope.row.id,1)">入库</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pamars.page" :page-sizes="[10, 20, 50, 100]" :page-size="pamars.limit" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <!--查看 -->
    <item-add-update v-if="addOrUpdateVisible" ref="itemAddUpdate" @updataList='updataList'></item-add-update>
    <!--出入库 -->
    <out-put-or-in-put v-if="outPutOrInPut" ref="outPutOrInPut" @updataList='updataList'></out-put-or-in-put>
  </div>
</template>

<script>
import itemAddUpdate from './item-add-update'
import outPutOrInPut from './out-put-or-in-put'
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      dataForm: {},
      pamars: {
        page: 1,
        limit: 10,
        goods_group_id: null,
        added: '',
        title: ''
      },
      totalPage: 0,
      dataList: [],
      options: [
        {
          value: 1,
          label: '上架'
        }, {
          value: 0,
          label: '下架'
        }
      ],
      goodsGroupsList: [], // 单品分组
      dataListLoading: false,
      addOrUpdateVisible: false, // 添加，编辑
      outPutOrInPut: false// 出入库
    }
  },
  components: {
    itemAddUpdate,
    outPutOrInPut
  },
  mounted () {
    this.getList()
    this.getGoodsGroups()
  },
  methods: {
    switchChange (row) {
      let msg = ''
      if (row.added) {
        msg = '已上架'
      } else {
        msg = '已下架'
      }
      seviceApi.singlegoodsUpDown(row.id).then(res => {
        if (res.data && res.data.code == 200) {
          this.$message.success(msg)
        } else {
          row.added = !row.added
        }
      })
    },
    updataList (e) {
      this.getList()
    },
    getTemplateRow (value) {
      this.pamars.goods_group_id = value[1]
    },
    // 获取单品分组
    getGoodsGroups () {
      seviceApi.getGoodsGroupsList().then(res => {
        if (res && res.data.code == 200) {
          // var arry = []
          // for (var i = 0; i < res.data.list.length; i++) {
          //   arry = arry.concat(res.data.list[i].children)
          // }
          for (var i = 0; i < res.data.list.length; i++) {
            if (res.data.list[i].parent_id == 0) {
              res.data.list[i].value = res.data.list[i].id
              res.data.list[i].label = res.data.list[i].name
            }
            for (var k = 0; k < res.data.list[i].children.length; k++) {
              if (res.data.list[i].children[k].parent_id > 0) {
                res.data.list[i].children[k].value = res.data.list[i].children[k].id
                res.data.list[i].children[k].label = res.data.list[i].children[k].name
                delete res.data.list[i].children[k].children
              }
            }
          }
          this.goodsGroupsList = res.data.list
        }
      })
    },
    getList () {
      seviceApi.getSinglegoodsList(this.pamars).then(res => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.page.list
          this.totalPage = res.data.page.totalCount
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pamars.limit = val
      this.getList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pamars.page = val
      this.getList()
    },
    // 查询
    getDataList () {
      this.pamars.page = 1
      this.getList()
    },
    // 添加，编辑
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.itemAddUpdate.init(id)
      })
    },
    // 出入库
    goOutPutOrInPut (id, type) {
      this.outPutOrInPut = true
      this.$nextTick(() => {
        this.$refs.outPutOrInPut.init(id, type)
      })
    },
    // 删除
    del (id) {
      this.$confirm(`确定要删除该条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        seviceApi.delSinglegoods(id).then(res => {
          if (res && res.data.code == 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getList()
          }
        })
      }).catch(() => { })
    }
  }
}
</script>
